package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.order.strategy.TradeDataStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component("1001")
public class AlbumTradeDataStrategy implements TradeDataStrategy {
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Override
    public void handlerTradeData(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        //1.1 初始化订单结算页三个价格：原价、减免价、订单价
        BigDecimal originalAmount = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");
        BigDecimal orderAmount = new BigDecimal("0.00");

        //1.2 初始化订单结算页两个集合：商品明细、商品优惠集合
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        //3  处理购买商品类别：专辑
        Long albumId = tradeVo.getItemId();
        Boolean isPaid = userFeignClient.isPaidAlbum(albumId).getData();
        //3.1 判断用户是否购买过该专辑
        Assert.state(!isPaid, "用户{}已购买专辑{}", userId, albumId);
        //3.2 远程调用专辑服务获取专辑详情
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", albumId);
        BigDecimal price = albumInfo.getPrice();
        BigDecimal discount = albumInfo.getDiscount();
        BigDecimal vipDiscount = albumInfo.getVipDiscount();

        //3.3 远程调用用户服务判断用户是否为VIP
        Boolean isVip = false;
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
        Assert.notNull(userInfoVo, "用户：{}不存在！", userId);
        if (userInfoVo.getIsVip().intValue() == 1 && userInfoVo.getVipExpireTime().after(new Date())){
            isVip = true;
        }
        //3.4 计算订单相关价格,暂时让订单价等于原价
        originalAmount = albumInfo.getPrice();
        orderAmount = originalAmount;
        //减免价=原价*折扣
        if (!isVip && albumInfo.getDiscount().intValue() != -1) {
            //普通用户 且 专辑有普通用户折扣
            orderAmount = originalAmount.multiply(albumInfo.getDiscount()).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
        }

        if (isVip && albumInfo.getVipDiscount().intValue() != -1) {
            //VIP用户 且 专辑有VIP用户折扣
            orderAmount = originalAmount.multiply(albumInfo.getVipDiscount()).divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
        }
        //减免价=原价-订单价
        if (orderAmount.compareTo(originalAmount) != 0) {
            derateAmount = originalAmount.subtract(orderAmount);
        }
        //3.5 封装订单明细列表、优惠列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(albumId);
        orderDetailVo.setItemName("专辑: " + albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        if (orderAmount.compareTo(originalAmount) != 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("专辑限时优惠：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

    }
}
